//1.定义LinkList类
class Node{
    constructor(element){
        //保存元素
        this.element = element;
        //指向下个节点
        this.next = undefined;

    }
}

class LinkedList{
    constructor(){
        this.head = undefined;
        this.length = 0;
    }

    //1.从链表尾部插入元素

    push(element){
        const newNode = new Node(element);
        let current;
        //分情况讨论
        //1.插入值为第一个元素
        if(this.head == null){
            this.head = newNode;
        }
        else{
            current = this.head;
            while(current.next  != null){
                current = current.next;

            }
            current.next = newNode;     
        }
        this.length++;


    }
    //2.从中间任意位置插入元素
    insert(elenment,index){
        //检查是否越界
        if(index<0||index>this.length){
            return false;
        }
        else{
            //创建新的Node
            const newNode = new Node(elenment);
            //位置为0
            if(index === 0){
                const current = this.head;
                newNode.next = current;
                this.head = newNode;

            }
            //2.中间或者尾部进行插入
            else{
                let index2 = 0;
                let previous = this.getElementAt(index-1);
                let current = previous.next;
              /*   let current = this.head;
                let previous = null;
                while(index2++<index){
                    previous = current;
                    current = current.next;

                }*/
                previous.next = newNode;
                newNode.next = current; 
            }
            this.length++;
            return true;

        }
        

    }

    //3.根据位置获取元素
    getElementAt(index){
        if(index<0||index>this.length-1){
            return undefined;
        }
        let index2 =0;
        let current = this.head;
        while(index2++<index){
            current = current.next;
        }
        return current;
    }
    //4.返回一个值的位置
    indexOf(element){
        let current = this.head;
        let index =0;
        while(current){
            if(current.element === element){
                return index;
            }
            index++;
            current = current.next;
        }
        return -1;
    }
    //5.从指定位置移除元素
    removeAt(index){
        if(index<0||index>this.length){
            return undefined;
        }
        //分情况讨论
        let current = this.head ;
        //1.移除第一项
        if(index === 0){
            this.head = current.next;
        }
        //2.移除其它项
        else{
            let previous = this.getElementAt(index-1);
             current = this.getElementAt(index);
            previous.next = current.next;
        }
        this.length--;
        return current;

    }
    //6.修改某个位置的元素
    update(element,index){
      const result =  this.removeAt(index);
        this.insert(element,index);
        return result;
    }
    //7.移除某一项
    remove(elenment){
        //发现位置
       const index =  this.indexOf(elenment);
       if(index === -1){
           return;
       }
       this.removeAt(index);

    }
    //是否为空
    isEmpty(){
        return this.length === 0;
    }
    //返回元素个数
    size(){
        return this.length;
    }
    //返回头部元素
    getHead(){
        return this.head;
    }

    //将对象转换为字符串
    toString(){
        if(this.head == null) {
            return '';
        }
      //  let str = '';
        //let str = JSON.stringify(this.head.element)
        let str = this.head.element;
        let current = this.head.next;
        while(current!=null){
            str = str +','+current.element;
            current = current.next;

        }
        return str;

        /*if(this.head == null){
            return '';
        }
        let str =`${this.head.element}`;
        let current = this.head.next;
        while(current!=null){
            str= `${str},${current.element}`;
            current = current.next;
        }

        return str;*/
       
    }


}
//对功能进行测试
//1.初始化数据
const linkedList = new LinkedList();
linkedList.push('a');
linkedList.push('b');
linkedList.push('c');
console.log(linkedList.toString());
//console.log(JSON.stringify(linkedList));
//2.对任意位置插入进行测试；
linkedList.insert('1',0);
linkedList.insert('f',2);
//3.获取位置元素测试；
console.log(linkedList.getElementAt(2));
//4.测试返回一个值的位置
console.log(linkedList.indexOf('f'));
console.log(linkedList.indexOf('m'));
//5从指定位置移除元素
console.log(linkedList.removeAt(2));
//6.测试更新
console.log(linkedList.update('2',3));
//7.移除
linkedList.remove('b');
console.log(linkedList);